﻿package com.easyfun.healthmagicbox.gallary;

import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.comm.PictureDeleteTask;
import com.easyfun.healthmagicbox.inputviews.TakeAPictureActivity;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.widget.DialogBuilder;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class GridViewActivity extends BaseActivity{
	private DisplayMetrics dm;
	private GridImageAdapter ia;
	private GridView g;
	private int imageCol = 3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mygridview);

		//personal ID
		if (!checkPersonId()) return;
		ia = new GridImageAdapter(this,m_personid);
		g = (GridView) findViewById(R.id.myGrid);
		g.setAdapter(ia);
		g.setOnItemClickListener(new OnItemClick(this));
		// 得到屏幕的大小
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		Button butPic = (Button) findViewById(R.id.butTakePic);
		butPic.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent_pic = new Intent(
						GridViewActivity.this, TakeAPictureActivity.class );//PictureTessActivy.class
				intent_pic.putExtra(ConstantData.HMBPERSONID, m_personid);
				GridViewActivity.this.startActivity(intent_pic);
			}
			
		});
		g.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				new DialogBuilder(GridViewActivity.this)
				.setTitle("提示")
				.setMessage("删除此照片?")
				.setButtons("确定","取消", new DialogBuilder.OnDialogButtonClickListener() {
					@Override
					public void onDialogButtonClick(Context context, DialogBuilder builder, Dialog dialog, int dialogId, int which) {
						if (which == BUTTON_LEFT) {
							int syncID = ImageSource.getInstance(GridViewActivity.this)
									.deleteImageAt(position);
							if ((syncID!=-1) && (syncID!=0))
							{
								PictureDeleteTask task = new PictureDeleteTask(
									GridViewActivity.this,new BasicCommCallback(),String.valueOf(syncID));
								HealthThreadPool.getInstance().execute(task);
								g.invalidateViews();
							}
						}
					}
					
				}).create().show(); 
				
				return true;
			}
		});
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus)
		{
			//setupView();
			// update grid view 
			ImageSource.getInstance(GridViewActivity.this).imageCount(m_personid);
			g.invalidateViews();
		}
	}
	/**
	 * 屏幕切换时进行处理 如果屏幕是竖屏，则显示3列，如果是横屏，则显示4列
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		try {

			super.onConfigurationChanged(newConfig);
			// 如果屏幕是竖屏，则显示3列，如果是横屏，则显示4列
			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
				imageCol = 4;
			} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				imageCol = 3;
			}
			g.setNumColumns(imageCol);
			g.setAdapter(new ImageAdapter(this,m_personid));
			// ia.notifyDataSetChanged();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 
	 * @author 空山不空 点击具体的小图片时，会链接到GridViewActivity页面，进行加载和展示
	 */
	public class OnItemClick implements OnItemClickListener {
		public OnItemClick(Context c) {
			mContext = c;
		}

		@Override
		public void onItemClick(AdapterView aview, View view, int position,
				long arg3) {
			Intent intent = new Intent();
			intent.setClass(GridViewActivity.this, GalleryActivity.class);
			
			intent.putExtra("position", position);
			intent.putExtra(ConstantData.HMBPERSONID, m_personid);
			GridViewActivity.this.startActivity(intent);
		}

		private Context mContext;
	}

	/**
	 * 
	 * @author 空山不空 设置GridView的图片适配器
	 */
	public class GridImageAdapter extends BaseAdapter {

		Drawable btnDrawable;
		String personID;

		public GridImageAdapter(Context c, String personID) {
			mContext = c;
			this.personID = personID;
			Resources resources = c.getResources();
			btnDrawable = resources.getDrawable(R.drawable.main_bg);
		}

		public int getCount() {
			return ImageSource.getInstance(mContext).imageCount(personID);
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageViewExt imageView;

			if (convertView == null) {
				imageView = new ImageViewExt(mContext);
				// 如果是横屏，GridView会展示4列图片，需要设置图片的大小
				if (imageCol == 4) {
					imageView.setLayoutParams(new GridView.LayoutParams(
							dm.heightPixels / imageCol - 6, dm.heightPixels
									/ imageCol - 6));
				} else {// 如果是竖屏，GridView会展示3列图片，需要设置图片的大小
					imageView.setLayoutParams(new GridView.LayoutParams(
							dm.widthPixels / imageCol - 6, dm.widthPixels
									/ imageCol - 6));
				}
				imageView.setAdjustViewBounds(true);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

				// imageView.setPadding(3, 3, 3, 3);
			} else {
				imageView = (ImageViewExt) convertView;
			}
			// 动画效果
			// Animation an=
			// AnimationUtils.loadAnimation(mContext,R.anim.zoom_enter );
			// imageView.setAnimation(an);

			// Resources res = getResources();
			// //将Drawable转化为Bitmap
			// Bitmap bitmap
			// =ImageUtils.drawableToBitmap(res.getDrawable(mThumbIds[position]));
			// //缩放图片
			// Bitmap zoomBitmap = ImageUtils.zoomBitmap(bitmap, 100,100);
			// //获取圆角图片
			// Bitmap roundBitmap =
			// ImageUtils.getRoundedCornerBitmap(zoomBitmap, 10.0f);

			// imageView.setImageBitmap(roundBitmap);
			// imageView.setImageResource(ImageSource.mThumbIds[position]);
			Bitmap img= ImageSource.getInstance(mContext).imageAt(
					position,personID);
			imageView.setImageBitmap(img);
			return imageView;
		}

		private Context mContext;

	}

	
}
